package com.lkd.shoppingcart.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lkd.shoppingcart.entity.Page;
import com.lkd.shoppingcart.entity.Product;
import com.lkd.shoppingcart.service.ProductService;
import com.lkd.shoppingcart.service.impl.ProductServiceImpl;
import com.lkd.shoppingcart.utils.WebUtils;

@WebServlet("/admin/productServlet")
public class AdminProductServlet extends BaseServlet {
	
	private static final long serialVersionUID = 1L;
	private ProductService productService = new ProductServiceImpl();
	
	/**
	 * 获取页面信息
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        Page<Product> page = productService.page(pageNo,pageSize);
        page.setUrl("admin/productServlet?action=page");
        req.setAttribute("page",page);
        req.getRequestDispatcher("/pages/admin/product_manager.jsp").forward(req,resp);
    }
    
    /**
     * 添加商品
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 0);
        pageNo+=1;
        Product product = WebUtils.copyParamToBean(req.getParameterMap(),new Product());
        productService.addProduct(product);
        resp.sendRedirect(req.getContextPath() + "/admin/productServlet?action=page&pageNo=" + pageNo);
    }

    /**
     * 删除商品
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        productService.deleteProductById(id);
        resp.sendRedirect(req.getContextPath() + "/admin/productServlet?action=page&pageNo=" + req.getParameter("pageNo"));
    }

    /**
     * 修改商品
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Product product = WebUtils.copyParamToBean(req.getParameterMap(),new Product());
        productService.updateProduct(product);
        resp.sendRedirect(req.getContextPath() + "/admin/productServlet?action=page&pageNo=" + req.getParameter("pageNo"));
    }

    /**
     * 获取商品信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getProduct(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        Product product = productService.queryProductById(id);
        req.setAttribute("product", product) ;
        req.getRequestDispatcher("/pages/admin/product_edit.jsp").forward(req,resp);
    }

    /**
     * 获取商品列表
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Product> products = productService.queryProducts();
        req.setAttribute("products", products);
        req.getRequestDispatcher("/pages/admin/product_manager.jsp").forward(req,resp);
    }

}
